package com.lemon.core.base.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ArrayUtil;

import java.util.Map;

/**
 * 数据校验工具类
 *
 * @author pan
 * @since 2018-05-13
 */
public class CheckUtils {

    private CheckUtils() {
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * 断言对象不为null
     *
     * @param reference 判断的对象
     * @return 断言正确返回true, 否则为false
     */
    public static <T> boolean isNotNull(T reference) {
        return reference == null;
    }

    public static <T> boolean isNull(T reference) {
        return !isNotNull(reference);
    }

    /**
     * 断言集合不为null
     *
     * @param collection 判断的集合
     * @return 断言正确返回true, 否则为false
     */
    public static <T> boolean isNotNull(Iterable<T> collection) {
        return CollUtil.isNotEmpty(collection);
    }

    public static <T> boolean isNull(Iterable<T> collection) {
        return !isNotNull(collection);
    }

    /**
     * 断言集合不为null且不为空
     *
     * @param coll 判断的集合
     * @return 断言正确返回true, 否则为false
     */
    public static <T> boolean isNotEmpty(Iterable<T> coll) {
        return !CollUtil.isEmpty(coll);
    }

    public static <T> boolean isEmpty(Iterable<T> coll) {
        return !isNotEmpty(coll);
    }

    /**
     * 断言map不为null且不为空
     *
     * @param map 判断的map
     * @return 断言正确返回true, 否则为false
     */
    public static <K, V> boolean isNotEmpty(Map<K, V> map) {
        return CollUtil.isNotEmpty(map);
    }

    public static <K, V> boolean isEmpty(Map<K, V> map) {
        return !isNotEmpty(map);
    }

    /**
     * 断言数组不能为空
     *
     * @param arr 判断的数组
     * @return 断言正确返回true, 否则为false
     */
    public static <T> boolean isNotEmpty(T[] arr) {
        return ArrayUtil.isNotEmpty(arr);
    }

    public static <T> boolean isEmpty(T[] arr) {
        return !isNotEmpty(arr);
    }

    /**
     * 断言字符串不为null，不为空
     *
     * @param cs 判断的字符串
     * @return 断言正确返回true, 否则为false
     */
    public static boolean isNotEmpty(CharSequence cs) {
        return CharSequenceUtil.isNotEmpty(cs);
    }

    public static boolean isEmpty(CharSequence cs) {
        return !isNotEmpty(cs);
    }

    /**
     * 断言字符串不为null，不为空，不是全空格
     *
     * @param cs 判断的字符串
     * @return 断言正确返回true, 否则为false
     */
    public static boolean isNotBlank(CharSequence cs) {
        return CharSequenceUtil.isNotBlank(cs);
    }

    public static boolean isBlank(CharSequence cs) {
        return CharSequenceUtil.isBlank(cs);
    }

    public static boolean isGreaterThan(Number a, Number b) {
        if (a == null || b == null) {
            throw new IllegalArgumentException("Invalid number parameter, must not be null.");
        }
        return a.doubleValue() - b.doubleValue() > 0;
    }

    public static boolean isGreaterOrEqualThan(Number a, Number b) {
        if (a == null || b == null) {
            throw new IllegalArgumentException("Invalid number parameter, must not be null.");
        }
        return a.doubleValue() > b.doubleValue() || a.doubleValue() == b.doubleValue();
    }

    public static boolean isNotEqual(Object a, Object b) {
        return !a.toString().equals(b.toString());
    }

}
